<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\ApprovalList;
use App\Models\ApprovalLog;
use App\Models\Grid;
use App\Models\BuildForm;
use App\Models\Villager;
use App\Models\HouseType;
use App\Models\ProtocolSet;
use App\Models\GridImg;
use App\Models\GridArea;
use App\Models\ApprovalSet;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;

// 审批
class ApprovalController extends Controller
{
    // 审批者审批列表
    public function approvalList(Request $request)
    {
        $validate = Validator::make($request->all(), [
            'type' => 'required|in:1,2,3',
        ], [
            'type.required' => '请输入类型',
            'type.in' => '请输入正确的类型',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $params = $validate->getData();

        $user = auth('api')->user()->id ?? 2;
        $project_id = auth('api')->user()->project_id ?? 1;

        $keywords = $request->input('keywords', ''); // 查询关键字
        $apply_status = $request->input('apply_status', ''); // 审批状态
        $template_id = $request->input('template_id', ''); // 审批类型

        try {
            $data = DB::table('approval_list as t1')
                ->select('t1.id as list_id', 't1.apply_reason', 't1.created_at', 't1.apply_type','t1.user_id','t1.apply_status', 't2.id as log_id', 't3.username', 't4.name', 't5.name as apply_name')
                ->leftJoin('approval_log as t2', 't2.list_id', '=', 't1.id')
                ->leftJoin('user as t3', 't3.id', '=', 't1.user_id')
                ->leftJoin('grid as t4', 't4.id', '=', 't1.grid_id')
                ->leftJoin('approval_template as t5', 't5.id', '=', 't1.apply_type')
                ->where('t1.deleted_at',null)
                ->where('t1.project_id',$project_id)
                ->when($keywords, function ($query) use ($keywords) {
                    $query->where('t4.name', '=', $keywords);
                })
                ->when($apply_status!==null, function ($query) use ($apply_status) {
                    $query->where('t1.apply_status', '=', $apply_status);
                })
                ->when($template_id, function ($query) use ($template_id) {
                    $query->where('t1.apply_type', '=', $template_id);
                })
                ->when($params['type'] == 1, function ($query) use ($user) {//待审批
                    $query->where('t2.apply_user_id', $user)
                    ->whereRaw('t1.step=t2.step')
                    ->whereIn('t1.apply_status',[0,3])
                    ->where('t2.apply_result', '');
                })
                ->when($params['type'] == 2, function ($query) use ($user) {//已审批
                    $query->where('t2.apply_user_id', $user)
                    ->where('t2.apply_result', '<>', '');
                })
                ->when($params['type'] == 3, function ($query) use ($user) {//我发起的
                    $query->where('t1.user_id', $user)
                    ->whereRaw('t1.step=t2.step');
                })
                ->orderByDesc('t1.id')
                ->paginate(20);

                $num['log_1']=$this->getApplyNum($user,$project_id,$keywords,$apply_status,$template_id,1);//待审批
                $num['log_2']=$this->getApplyNum($user,$project_id,$keywords,$apply_status,$template_id,2);//已审批
                $num['list_']=$this->getApplyNum($user,$project_id,$keywords,null,$template_id,3);//我发起的-全部
                $num['list_0']=$this->getApplyNum($user,$project_id,$keywords,0,$template_id,3);//我发起的-0申请中
                $num['list_3']=$this->getApplyNum($user,$project_id,$keywords,3,$template_id,3);//我发起的-3审批中
                $num['list_1']=$this->getApplyNum($user,$project_id,$keywords,1,$template_id,3);//我发起的-1通过
                $num['list_2']=$this->getApplyNum($user,$project_id,$keywords,2,$template_id,3);//我发起的-2拒绝
               
                $li=['data'=>$data,'num'=>$num];

            return showData($li);
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
    }


    public function getApplyNum($user,$project_id,$keywords,$apply_status,$template_id,$type){
        $num = DB::table('approval_list as t1') 
        ->leftJoin('approval_log as t2', 't2.list_id', '=', 't1.id')  
        ->leftJoin('grid as t4', 't4.id', '=', 't1.grid_id')
        ->where('t1.deleted_at',null)
        ->where('t1.project_id',$project_id)
        ->when($keywords, function ($query) use ($keywords) {
            $query->where('t4.name', '=', $keywords);
        })
        ->when($apply_status!==null, function ($query) use ($apply_status) {
            $query->where('t1.apply_status', '=', $apply_status);
        })
        ->when($template_id, function ($query) use ($template_id) {
            $query->where('t1.apply_type', '=', $template_id);
        })
        ->when($type == 1, function ($query) use ($user) {//待审批
            $query->where('t2.apply_user_id', $user)
            ->whereRaw('t1.step=t2.step')
            ->whereIn('t1.apply_status',[0,3])
            ->where('t2.apply_result', '');
        })
        ->when($type == 2, function ($query) use ($user) {//已审批
            $query->where('t2.apply_user_id', $user)
            ->where('t2.apply_result', '<>', '');
        })
        ->when($type == 3, function ($query) use ($user) {//我发起的
            $query->where('t1.user_id', $user)
            ->whereRaw('t1.step=t2.step');
        })
        ->count(); 

        return $num;
    }

    // 审批操作
    public function addApproval(Request $request)
    {
        $validate = Validator::make($request->all(), [
            'list_id' => 'required',
            'log_id' => 'required',
            'apply_result' => 'required|in:1,2',
        ], [
            'list_id.required' => '请输入列表id',
            'log_id.required' => '请输入详情id',
            'apply_result.required' => '请输入审批结果',
            'apply_result.in' => '请输入正确的审批结果',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $params = $validate->getData();

        try {
            $list = ApprovalList::query()->where('id', $params['list_id'])->first();
            if ($params['apply_result'] == 1) { //同意
                $now_step = $list['step'];

                $find_id = ApprovalLog::query()
                    ->where('list_id', $params['list_id'])
                    ->where('step', '>', $now_step)
                    ->value('id');

                if ($find_id) { //不是最后一步
                    $listData['apply_status'] = 3;

                    $content = ApprovalSet::query()->where('id',$list['set_id'])->value('content');
                    $content_arr =json_decode($content,true);
                    $need_num=$content_arr[$now_step-1]['need_num'];

                    $count = ApprovalLog::query()
                            ->where('list_id', $params['list_id'])
                            ->where('step', $now_step)
                            ->where('apply_result', 1)
                            ->count();
                    if($need_num==$count+1){
                        $listData['step'] = $now_step + 1;
                    }        

                } else { //最后一步
                    $listData['apply_status'] = 1;
                    $content = json_decode($list['content'], true);
                    if($content){
                        $request->merge($content); //合并参数
                    }
                    switch ($list['apply_type']) {
                        case '1': //更新用户信息
                            $obj = new  AuthController($request);
                            $data = $obj->updateInfo($request);
                            break;
                        case '2': //身份证
                            $obj = new  GridAreaController($request);
                            $data = $obj->addIdCard($request);
                            break;
                        case '3': //户口本
                            $obj = new  GridImgController($request);
                            $data = $obj->addImg($request);
                            break;
                        case '4': //土地证
                            $obj = new  GridImgController($request);
                            $data = $obj->addImg($request);
                            break;
                        case '5': //面积确认表
                            $obj = new  GridAreaController($request);
                            $data = $obj->addArea($request);
                            break;
                        case '6': //测绘
                            $obj = new  GridAreaController($request);
                            $data = $obj->addArea($request);
                            break;
                        case '7': //评估
                            $obj = new  GridAssessController($request);
                            $data = $obj->addAssess($request);
                            break;
                        case '8': //户型
                            $obj = new  GridHouseController($request);
                            $data = $obj->addHouse($request);
                            break;
                        case '9': //签约
                            $obj = new  GridImgController($request);
                            $data = $obj->addImg($request);
                            break;
                        case '10': //腾房
                            $obj = new  GridImgController($request);
                            $data = $obj->addImg($request);
                            break;
                        case '11': //结算
                            $obj = new  GridPriceController($request);
                            $data = $obj->addPrice($request);
                            break;
                        case '12': //银行
                            $obj = new  GridPriceController($request);
                            $data = $obj->addPrice($request);
                            break;
                        case '13': //发放
                            $obj = new  GridPriceController($request);
                            $data = $obj->addPrice($request);
                            break;
                        case '19': //完成签约
                            Grid::query()->where('id',$list['grid_id'])->update(['sign_status'=>1]);
                            break;
                        case '20': //完成腾房
                            Grid::query()->where('id',$list['grid_id'])->update(['vacate_room_status'=>1]);
                            break;
                        case '21': //完成结算
                            Grid::query()->where('id',$list['grid_id'])->update(['settlement_status'=>1]);
                            break;
                        case '22': //完成银行
                            Grid::query()->where('id',$list['grid_id'])->update(['bank_status'=>1]);
                            break;
                        case '23': //完成发放
                            Grid::query()->where('id',$list['grid_id'])->update(['grant_status'=>1]);
                            break;          
                        case '24': //完成户口本
                            $this->setRuhuStatus($list['grid_id'],'hukou_status');
                            break;
                        case '25': //完成土地证
                            $this->setRuhuStatus($list['grid_id'],'fangchan_status');
                            break;
                        case '26': //完成测绘
                            Grid::query()->where('id',$list['grid_id'])->update(['mapping_status'=>1]);
                            break;
                        case '27': //完成评估
                            Grid::query()->where('id',$list['grid_id'])->update(['assess_status'=>1]);
                            break;       
                    }
                }

                $data = ApprovalList::query()
                    ->where('id', $params['list_id'])
                    ->update($listData);
            } else { //拒绝
                $listData['apply_status'] = 2;
                $data = ApprovalList::query()
                    ->where('id', $params['list_id'])
                    ->update($listData);
            }

            // 删除同类型之前审批
            $delete = ApprovalList::query()
            ->where('apply_type', $list['apply_type'])
            ->where('grid_id', $list['grid_id'])
            ->where('id', '<>',$params['list_id'])
            ->delete();   

            $logData['apply_result'] = $params['apply_result'];
            $logData['approval_reason'] = $params['approval_reason'];
            $logData['approval_time'] = date('Y-m-d H:i:s');
            $data = ApprovalLog::query()
                ->where('id', $params['log_id'])
                ->update($logData);

            return showSuccess('提交成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
    }

    // 审批详情
    public function approvalDetails(Request $request)
    {
        $validate = Validator::make($request->all(), [
            'list_id' => 'required',
            'log_id' => 'required',
        ], [
            'list_id.required' => '请输入列表id',
            'log_id.required' => '请输入log_id',
        ]);

        if ($validate->fails()) {
            return showError($validate->errors()->first());
        }

        $params = $validate->getData();

        $status = ['申请中', '通过', '拒绝','审批中'];
        $list = ApprovalList::query()
            ->select('apply_reason', 'apply_status', 'content', 'grid_id','apply_type','step')
            ->where('id', $params['list_id'])
            ->first();

        $content = json_decode($list['content'], true);
        $grid_id = $list['grid_id'];
        $template_id=$list['apply_type'];

        switch ($template_id) {
            case 1: //更改用户信息
                $data = $content;
                break;
            case 2: //修改身份证
                if ($content['villager_list']) {
                    $content['villager'] = json_decode($content['villager_list'], true);
                    foreach ($content['villager'] as &$v) {
                        $v['id_obverse_img'] = $v['id_obverse_img'] ? $this->getImageUrl($v['id_obverse_img']) : [];
                        $v['id_reverse_img'] = $v['id_reverse_img'] ? $this->getImageUrl($v['id_reverse_img']) : [];
                    }
                } else {
                    $content['villager'] = [];
                }

                if ($content['agent_list']) {
                    $content['agent'] = json_decode($content['agent_list'], true);
                    $content['agent']['id_obverse_img'] = isset($content['agent']['id_obverse_img']) ? $this->getImageUrl($content['agent']['id_obverse_img']) : [];
                    $content['agent']['id_reverse_img'] = isset($content['agent']['id_reverse_img']) ? $this->getImageUrl($content['agent']['id_reverse_img']) : [];
                } else {
                    $content['agent'] = [];
                }

                $data = $content;
                break;
            case 3: //修改户口本
                $data = $this->getImgData($content, $grid_id);
                break;
            case 4: //修改土地证
                $data = $this->getImgData($content, $grid_id);
                break;
            case 5: //修改面积表
                $data = $this->getAreaData($content, $grid_id);
                break;
            case 6: //修改测绘
                $data = $this->getAreaData($content, $grid_id);
                break;
            case 7: //修改评估
                $data=$content;
                $data['assess_imgs']=$data['assess_imgs']?$this->getImageUrl($data['assess_imgs'],1):[];
                $data['house_use']=Grid::query()->where('id',$grid_id)->value('house_use');
                break;
            case 8: //修改户型
                $type=$this->getAreaType($grid_id);
                if($type!=1&&$type!=2){
                    return $type;
                }
                $data=Grid::query()
                ->select('id','name')
                ->where('id',$grid_id)
                ->with('villager:id,name,identity_card,phone,grid_id')
                ->with(['grid_area'=> function ($query) use ($type) {
                    $query->select('supply_area','reward_area','grid_id')->where('type', $type);
                }])
                ->first();

                $house_type = json_decode($content['house_type'], true);
                foreach ($house_type as $k => $v) {
                    $li[$k]['is_decorate'] = $v['is_decorate'];
                    $li[$k]['house_type'] = HouseType::query()->select('id', 'name', 'area')->where('id', $v['house_type_id'])->first()->toArray();
                    $li[$k]['villager'] = Villager::query()->select('id', 'name')->whereIn('id', explode(',', $v['villager_ids']))->get()->toArray();
                }
        
                $data['grid_house'] = $li;
                break;
            case 9: //修改签约
                $data = $this->getImgData($content, $grid_id);
                break;
            case 10: //修改腾房
                $data = $this->getImgData($content, $grid_id,6);
                break;
            case 11: //修改结算
                $data = $this->getPriceData($content, $grid_id,1);
                break;
            case 12: //修改银行
                $data = $this->getPriceData($content, $grid_id,2);
                break;
            case 13: //修改发放
                $data = $this->getPriceData($content, $grid_id,3);
                break;
            case 14: //无异议
                $data = $this->getTableData($grid_id);
                break;
            case 15: //有异议
                $data = $this->getTableData($grid_id);
                break;
            case 16: //兼用面积
                $data = $this->getTableData($grid_id);
                break;
            case 17: //有建房手续
                $data = $this->getTableData($grid_id);
                break;
            case 18: //无建房手续
                $data = $this->getTableData($grid_id);
                break;
            case 19: //完成签约
                $merge['grid_id']=$grid_id;
                $merge['type']=5;
                $request->merge($merge); //合并参数
                $obj = new  GridImgController($request);
                $data = $obj->imgList($request)['data'];
                break;
            case 20: //完成腾房
                $merge['grid_id']=$grid_id;
                $merge['type']=6;
                $request->merge($merge); //合并参数
                $obj = new  GridImgController($request);
                $data = $obj->imgList($request)['data'];
                break;
            case 21: //完成结算
                $merge['grid_id']=$grid_id;
                $merge['type']=1;
                $request->merge($merge); //合并参数
                $obj = new  GridPriceController($request);
                $data = $obj->priceList($request)['data'];
                break;
            case 22: //完成银行
                $merge['grid_id']=$grid_id;
                $merge['type']=2;
                $request->merge($merge); //合并参数
                $obj = new  GridPriceController($request);
                $data = $obj->priceList($request)['data'];
                break;
            case 23: //完成发放
                $merge['grid_id']=$grid_id;
                $merge['type']=3;
                $request->merge($merge); //合并参数
                $obj = new  GridPriceController($request);
                $data = $obj->priceList($request)['data'];
                break;
            case 24: //完成户口本
                $data = $this->getCompleteImgData($grid_id);
                break;
            case 25: //完成土地证
                $data = $this->getCompleteImgData($grid_id);
                break;    
            case 26: //完成测绘
                $merge['grid_id']=$grid_id;
                $merge['type']=2;
                $request->merge($merge); //合并参数
                $obj = new  GridAreaController($request);
                $data = $obj->areaList($request)['data'];
                break;
            case 27: //完成评估
                $merge['grid_id']=$grid_id;
                $request->merge($merge); //合并参数
                $obj = new  GridAssessController($request);
                $data = $obj->assessList($request)['data'];
                break;    
        }

        $data['apply_reason'] = $list['apply_reason'];
        $data['apply_status'] = $list['apply_status'];
        $data['apply_status_str'] = $status[$data['apply_status']];

        $log= ApprovalLog::query()
            ->select('approval_reason','apply_result','step')
            ->where('id', $params['log_id'])
            ->first();
        $data['approval_reason'] = $log['approval_reason'];
        $data['apply_result'] = $log['apply_result']; 

        $data['if_last'] = $list['step']==$log['step']?1:0;    

        return showData($data);
    }

    public function getAreaData($content, $grid_id)
    {
        $data = Grid::query()
            ->select('id', 'name', 'house_use', 'supply_form', 'build_form', 'street_id', 'village_id', 'project_id', 'segment_id', 'address', 'business_num', 'business_award_time', 'business_imgs')
            ->where('id', $grid_id)
            ->with('villager:id,name,identity_card,phone,grid_id')
            ->with('agent:name,identity_card,phone,grid_id')
            ->first()
            ->toArray();

        if($content['house_type']){
            $house_type = json_decode($content['house_type'], true);
            foreach ($house_type as $k => $v) {
                $li[$k]['is_decorate'] = $v['is_decorate'];
                $li[$k]['house_type'] = HouseType::query()->select('id', 'name', 'area')->where('id', $v['house_type_id'])->first()->toArray();
                $li[$k]['villager'] = Villager::query()->select('id', 'name')->whereIn('id', explode(',', $v['villager_ids']))->get()->toArray();
            }
        }    
        
        $data['grid_house'] = $li??[];
        $data['grid_area'] = $content;

        $data['business_imgs'] = isset($content['business_imgs']) ? $this->getImageUrl($content['business_imgs']) : [];
        $data['address'] = $content['address'];
        $data['build_form_name'] = BuildForm::query()->where('id', $data['build_form'])->value('name');
        return $data;
    }

    public function getImgData($content, $grid_id,$template_id='')
    {
        $data = Grid::query()
            ->select('id', 'name', 'house_use', 'supply_form', 'build_form', 'street_id', 'village_id', 'project_id', 'segment_id')
            ->where('id', $grid_id)
            ->with('villager:id,name,identity_card,phone,grid_id')
            ->first();

        $imgs_data = json_decode($content['imgs_data'], true);
        foreach ($imgs_data as $k => $vv) {
            $li[$k]['imgs'] = $vv['imgs'] ? $this->getImageUrl($vv['imgs'], 1) : [];
            $li[$k]['villager_id'] = $vv['villager_id'] ?? 0;
            $li[$k]['num'] = $vv['num'] ?? 0;
            $li[$k]['type'] = $vv['type'];
        }
        $data['grid_img'] = $li;

        if ($data) {
            foreach ($data['grid_img'] as $v) {
                $li[$v['type']][] = $v;
            }
            $data['grid_img_arr'] = $li ?? [];
            $data['villager_name'] = implode(',', array_column(json_decode($data['villager']), 'name'));
        }

        if($template_id==6){
            $data['build_form_name']=BuildForm::query()->where('id',$data['build_form'])->value('name');

            $data['protocol_set']=ProtocolSet::query()
            ->select('id','require_clear_start_time','require_clear_end_time')
            ->where('supply_form',$data['supply_form'])
            ->where('build_form',$data['build_form'])
            ->where('street_id',$data['street_id'])
            ->where('village_id',$data['village_id'])
            ->where('project_id',$data['project_id'])
            ->where('segment_id',$data['segment_id'])
            ->first();

            if(empty($data['protocol_set'])){
                return showError('未查询到配置信息，请联系管理员到后台配置');
            }
        }

        unset($data['supply_form']);
        unset($data['build_form']);
        unset($data['street_id']);
        unset($data['village_id']);
        unset($data['project_id']);
        unset($data['segment_id']);

        return $data;
    }

    public function getPriceData($content, $grid_id,$type){
        $data=Grid::query()
        ->select('id','name','supply_form','build_form','street_id','village_id','project_id','segment_id')
        ->where('id',$grid_id)
        ->with('villager:name,identity_card,phone,grid_id')
        ->with('agent:name,identity_card,phone,grid_id')
        ->with(['grid_price'=> function ($query) use ($type) {
            $query->where('type', $type);
        }])
        ->first()
        ->toArray();

        isset($content['pay_fee'])?$data['grid_price']['pay_fee']=$content['pay_fee']:'';
        isset($content['pay_time'])?$data['grid_price']['pay_time']=$content['pay_time']:'';
        isset($content['num'])?$data['grid_price']['num']=$content['num']:'';
        isset($content['imgs'])?$data['grid_price']['imgs']=$content['imgs']:'';

        $data['grid_price']['imgs']=isset($data['grid_price']['imgs'])?$this->getImageUrl($data['grid_price']['imgs']):[];
        $data['build_form_name']=BuildForm::query()->where('id',$data['build_form'])->value('name');
       
        unset($data['street_id']);
        unset($data['village_id']); 
        unset($data['project_id']);
        unset($data['segment_id']);

        return $data;
    }

    public function getTableData($grid_id){
        $type = $this->getAreaType($grid_id);
        if ($type != 1 && $type != 2) {
            return $type;
        }
        $data = Grid::query()
            ->select('id','name','house_use','supply_form','build_form','business_num','business_imgs','business_award_time','address')
            ->with('villager:name,identity_card,phone,grid_id')
            ->with(['grid_area' => function ($query) use ($type) {
                $query->select('dualuse_area','use_area','build_area','two_area','basement_area','country_area','collective_area','yard_area','grid_id','supply_area','improve_area','reward_area','public_area')->where('type', $type);
            }])
            ->with('grid_img:num,type,imgs,grid_id,villager_id')
            ->where('id', $grid_id)
            ->first();

            foreach($data['grid_img'] as &$v){
                $v['imgs']=$v['imgs']?$this->getImageUrl($v['imgs'],1):[];
                $li[$v['type']][]=$v;
            }
            $data['grid_img_arr']=$li??[];

            $data['address']=$data['address'];
            $data['build_form_name'] = BuildForm::query()->where('id', $data['build_form'])->value('name');
            // 证载面积
            $data['grid_area_ruhu'] = GridArea::query()->where('grid_id',$data['id'])->where('type',1)->first();
            //   营业执照
            $data['business_imgs']=$data['business_imgs']?$this->getImageUrl($data['business_imgs'],1):'';

            unset($data['grid_img']);

        return $data;    
    }


    public function getCompleteImgData($grid_id)
    {
        $data=Grid::query()
        ->select('id','name','house_use','supply_form','build_form','street_id','village_id','project_id','segment_id','if_sign','for_user_id','if_mapping','if_build','sign_time')
        ->where('id',$grid_id)
        ->with('villager:id,name,identity_card,phone,grid_id')
        ->with('agent:name,identity_card,phone,grid_id')
        ->with('village:name,id')
        ->with('grid_img:num,type,imgs,grid_id,villager_id')
        ->first();

        if($data){
            foreach($data['grid_img'] as &$v){
                $v['imgs']=$v['imgs']?$this->getImageUrl($v['imgs'],1):[];
                $li[$v['type']][]=$v;
            }
            $data['grid_img_arr']=$li??[];
            $data['villager_name']=implode(',',array_column(json_decode($data['villager']),'name'));
        }

        return $data;
    }


     // 拒绝后新增面积表审批(废弃)
     public function addTableApproval(Request $request)
     {       
        try{
            $validate = Validator::make($request->all(), [
                'template_id' => 'required',
                'grid_id' => 'required',
            ], [
                'template_id.required' => '请输入审批表类型',
                'grid_id.required' => '请输入网格id',
            ]);

            if ($validate->fails()) {
                return showError($validate->errors()->first());
            }

            $params = $validate->getData();

            if(!$this->findApproval($params['grid_id'], $params['template_id'])){
                $res = $this->setApproval($params['grid_id'], $params['template_id']);
            } 

            return showSuccess('提交成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
     }

    //领导审批反悔功能
    public function cancelApproval(Request $request)
    {  
        try{
            $validate = Validator::make($request->all(), [
                'list_id' => 'required',
                'log_id' => 'required',
            ], [
                'list_id.required' => '请输入list_id',
                'log_id.required' => '请输入log_id',
            ]);

            if ($validate->fails()) {
                return showError($validate->errors()->first());
            }

            $params = $validate->getData();

            $listData['apply_status'] = 3;
            $data = ApprovalList::query()
            ->where('id', $params['list_id'])
            ->update($listData);

            $logData['apply_result'] = 0;
            $logData['approval_reason'] = '';
            $logData['approval_time'] = null;
            $data = ApprovalLog::query()
            ->where('id', $params['log_id'])
            ->update($logData);
            return showSuccess('提交成功！');
        } catch (\Exception $e) {
            return showError($e->getMessage());
        }
    }
}
